package com.hj.authority.service.impl;

import cn.hutool.core.util.StrUtil;

import com.hj.authority.dao.SysParamsDao;
import com.hj.authority.domain.cache.SysParamsCache;
import com.hj.authority.domain.entity.SysParams;
import com.hj.authority.service.SysParamsService;
import com.hj.mybatis.service.impl.BaseServiceImpl;
import jakarta.annotation.PostConstruct;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author hj
 * @Date 2023/4/27
 * @Description: 参数管理
 */
@Service
@AllArgsConstructor
public class SysParamsServiceImpl extends BaseServiceImpl<SysParamsDao, SysParams> implements SysParamsService {

    private final SysParamsCache sysParamsCache;

    @PostConstruct
    public void init() {
        // 查询列表
        List<SysParams> list = baseMapper.selectList(null);
        // 保存到缓存
        sysParamsCache.saveList(list);
    }


    @Override
    public String getString(String paramKey) {
        if (StrUtil.isBlank(paramKey)) {
            throw new RuntimeException("参数不能为空，paramKey：" + paramKey);
        }
        String value = sysParamsCache.get(paramKey);
        if (StrUtil.isBlank(value)) {
            throw new RuntimeException("没有查询到此key对应值，paramKey：" + paramKey);
        }
        return value;
    }

    @Override
    public boolean getBoolean(String paramKey) {
        String value = getString(paramKey);
        return Boolean.parseBoolean(value);
    }



}